package com.dxd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dxd.entity.MessageBoard;
import com.dxd.entity.UserInfo;
import com.dxd.utils.DbUtil;

public class MessageBoardDao extends PublicDao<MessageBoard> {

	@Override
	protected MessageBoard result(ResultSet rs) throws SQLException {
		MessageBoard board = new MessageBoard();
		return board;
	}

	@Override
	protected List<MessageBoard> results(ResultSet rs) throws SQLException {
		List<MessageBoard> messageBoards = new ArrayList<MessageBoard>();
		while (rs.next()) {
			MessageBoard messageBoard = new MessageBoard();
			messageBoard.setContent(rs.getString("content"));
			UserInfo firend = new UserInfo();
			firend.setId(rs.getInt("friend_id"));
			firend.setName(rs.getString("friend_name"));
			firend.setHeadImg(rs.getString("friend_head_img"));
			messageBoard.setFirend(firend);
			messageBoard.setId(rs.getInt("id"));
			messageBoard.setMessageId(rs.getInt("message_id"));
			messageBoard.setReadTime(rs.getString("read_time"));
			messageBoard.setSendTime(rs.getString("send_time"));
			messageBoard.setState(rs.getInt("state"));
			UserInfo user = new UserInfo();
			user.setId(rs.getInt("user_id"));
			user.setName(rs.getString("user_name"));
			user.setHeadImg(rs.getString("user_head_img"));
			messageBoard.setUser(user);
			List<MessageBoard> boards = new ArrayList<MessageBoard>();
			StringBuffer sql = new StringBuffer("SELECT ");
			sql.append(" a.*,");
			sql.append(" b.id AS user_id,");
			sql.append(" b.`name` AS user_name,");
			sql.append(" b.head_img AS user_head_img,");
			sql.append(" c.head_img AS friend_head_img,");
			sql.append(" c.id as friend_id,");
			sql.append(" c.name AS friend_name");
			sql.append(" FROM message_board a");
			sql.append(" LEFT JOIN user_info b ON a.user=b.id");
			sql.append(" LEFT JOIN user_info c ON a.friend=c.id");
			sql.append(" WHERE a.message_id=");
			sql.append(messageBoard.getMessageId());
			sql.append(" AND a.parent_id=");
			sql.append(messageBoard.getId());
			sql.append(" ORDER BY a.id ASC");
			con = DbUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			ResultSet rs2 = pst.executeQuery();
			while (rs2.next()) {
				MessageBoard board = new MessageBoard();
				board.setContent(rs2.getString("content"));
				board.setParentID(rs2.getInt("parent_id"));
				UserInfo firend2 = new UserInfo();
				firend2.setId(rs2.getInt("friend_id"));
				firend2.setName(rs2.getString("friend_name"));
				firend2.setHeadImg(rs2.getString("friend_head_img"));
				board.setFirend(firend2);
				board.setId(rs2.getInt("id"));
				board.setMessageId(rs2.getInt("message_id"));
				board.setReadTime(rs2.getString("read_time"));
				board.setSendTime(rs2.getString("send_time"));
				board.setState(rs2.getInt("state"));
				UserInfo user2 = new UserInfo();
				user2.setId(rs2.getInt("user_id"));
				user2.setName(rs2.getString("user_name"));
				user2.setHeadImg(rs2.getString("user_head_img"));
				board.setUser(user2);
				boards.add(board);
				board = null;
			}
			DbUtil.closeDB(rs2, pst, con);
			messageBoard.setChildrens(boards);
			messageBoards.add(messageBoard);
			messageBoard = null;
		}

		return messageBoards;
	}

	/**
	 * 根据消息id查询所有留言
	 * 
	 * @param messageId
	 * @return
	 */
	public List<MessageBoard> queryByMessageId(Integer messageId) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.*,");
		sql.append(" b.id AS user_id,");
		sql.append(" b.`name` AS user_name,");
		sql.append(" b.head_img AS user_head_img,");
		sql.append(" c.id as friend_id,");
		sql.append(" c.name AS friend_name,");
		sql.append(" c.head_img AS friend_head_img");
		sql.append(" FROM message_board a");
		sql.append(" LEFT JOIN user_info b ON a.user=b.id");
		sql.append(" LEFT JOIN user_info c ON a.friend=c.id");
		sql.append(" WHERE a.message_id=? AND a.parent_id=0 ORDER BY id ASC");
		List<Object> objs = new ArrayList<Object>();
		objs.add(messageId);
		List<MessageBoard> list = queryList(sql.toString(), objs);
		return list;
	}

	/**
	 * 添加留言
	 * 
	 * @param board
	 * @return
	 */
	public int addMessageBoard(MessageBoard board) {
		String sql = "INSERT INTO message_board(message_id,user,friend,send_time,content,state,parent_id) VALUES(?,?,?,?,?,?,?)";
		List<Object> objs = new ArrayList<Object>();
		objs.add(board.getMessageId());
		objs.add(board.getUser().getId());
		objs.add(board.getFirend().getId());
		objs.add(new Date());
		objs.add(board.getContent());
		objs.add(board.getState());
		objs.add(board.getParentID());
		return AddOrRemoveOrUpdate(sql, objs);
	}

}
